package models

type SysDictionaryDetail struct {
	ModelBase
	Label           string `json:"label" form:"label" gorm:"column:label;comment:展示值"`          // 展示值
	Value           int    `json:"value" form:"value" gorm:"column:value;comment:字典值"`          // 字典值
	Status          *bool  `json:"status" form:"status" gorm:"column:status;comment:启用状态"`      // 启用状态
	Sort            int    `json:"sort" form:"sort" gorm:"column:sort;comment:排序标记"`            // 排序标记
	SysDictionaryID int    `json:"sid" form:"sid" gorm:"column:sys_dictionary_id;comment:关联标记"` // 关联标记
}

type SysDictionaryDetailSearch struct {
	SysDictionaryDetail
	Page     int `json:"page" form:"current"`
	PageSize int `json:"pageSize" form:"pageSize"`
}

//@function: CreateSysDictionaryDetail
//@description: 创建字典详情数据
//@param: sysDictionaryDetail model.SysDictionaryDetail
//@return: err error

func CreateSysDictionaryDetail(sysDictionaryDetail SysDictionaryDetail) (err error) {
	err = db.Create(&sysDictionaryDetail).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSysDictionaryDetail
//@description: 删除字典详情数据
//@param: sysDictionaryDetail model.SysDictionaryDetail
//@return: err error

func DeleteSysDictionaryDetail(sysDictionaryDetail SysDictionaryDetail) (err error) {
	err = db.Delete(&sysDictionaryDetail).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateSysDictionaryDetail
//@description: 更新字典详情数据
//@param: sysDictionaryDetail *model.SysDictionaryDetail
//@return: err error

func UpdateSysDictionaryDetail(sysDictionaryDetail *SysDictionaryDetail) (err error) {
	err = db.Save(sysDictionaryDetail).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSysDictionaryDetail
//@description: 根据id获取字典详情单条数据
//@param: id uint
//@return: err error, sysDictionaryDetail model.SysDictionaryDetail

func GetSysDictionaryDetail(id uint) (err error, sysDictionaryDetail SysDictionaryDetail) {
	err = db.Where("id = ?", id).First(&sysDictionaryDetail).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSysDictionaryDetailInfoList
//@description: 分页获取字典详情列表
//@param: info request.SysDictionaryDetailSearch
//@return: err error, list interface{}, total int64

func GetSysDictionaryDetailInfoList(info SysDictionaryDetailSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := db.Model(&SysDictionaryDetail{})
	var sysDictionaryDetails []SysDictionaryDetail
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.Label != "" {
		db = db.Where("label LIKE ?", "%"+info.Label+"%")
	}
	if info.Value != 0 {
		db = db.Where("value = ?", info.Value)
	}
	if info.Status != nil {
		db = db.Where("status = ?", info.Status)
	}
	if info.SysDictionaryID != 0 {
		db = db.Where("sys_dictionary_id = ?", info.SysDictionaryID)
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&sysDictionaryDetails).Error
	return err, sysDictionaryDetails, total
}
